package com.pactera.asmp.server.common.bug.impl;

import java.util.HashMap;
import java.util.Map;

import com.pactera.asmp.server.entity.TestReportJiraDTO;
import com.pactera.asmp.server.entity.TestReportJiraEntity;
import org.apache.http.HttpStatus;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import com.alibaba.fastjson.JSONObject;
import com.pactera.asmp.server.entity.ProjectProductionConfig;
import com.pactera.asmp.server.pojo.TestReport;

@Service
public class TapdHandlerImpl extends AbstractBugHandler {

    private static final Logger logger = LoggerFactory.getLogger(TapdHandlerImpl.class);

    @Autowired
    private RestTemplate restTemplate;

    @Value("${minio.url}")
    private String fileUrl;

    @Value("${minio.bucketName}")
    private String buckName;

    /**
     * 上传bug信息到Tapd
     * 
     * @param title
     * @param stepContent
     * @param testReport
     * @param projectProductionConfig
     * 
     * @return
     */
    @Override
    public String[] handleBug(String title, String stepContent, TestReport testReport,
            ProjectProductionConfig projectProductionConfig) {
        // 校验输入的流水线信息
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.setBasicAuth(projectProductionConfig.getBugAccount(), projectProductionConfig.getBugPassword());
        org.springframework.http.HttpEntity<String> httpEntity = new org.springframework.http.HttpEntity<>(httpHeaders);
        Map<String, String> map = new HashMap<>();
        map.put("workspace_id", projectProductionConfig.getBugProjectId());
        map.put("title", title);
        map.put("reporter", "productionAutoTest");
        map.put("description", stepContent);
        // TODO 接入tapd的bug系统
        ResponseEntity<String> response = restTemplate.exchange(projectProductionConfig.getBugUrl(), HttpMethod.POST, httpEntity, String.class,
                map);
        if (HttpStatus.SC_OK != response.getStatusCodeValue()) {
            logger.error("{} request error. HttpStatus:{}. {}", projectProductionConfig.getBugUrl(), response.getStatusCodeValue(),
                    response.getBody());
        } else {
            String msg = response.getBody();
            try {
                JSONObject jsonObject = JSONObject.parseObject(msg);
                int status = Integer.parseInt(jsonObject.get("status").toString());
                if (status != 1) {
                    logger.error("{} request error. HttpStatus:{}. {}", projectProductionConfig.getBugUrl(), response.getStatusCodeValue(),
                            response.getBody());
                }
            } catch (Exception ex) {
                logger.error("{} request error. HttpStatus:{}. {}", projectProductionConfig.getBugUrl(), response.getStatusCodeValue(),
                        response.getBody());
            }
        }
        return null;
    }

    @Override
    public String[] handleBugForMonkey(String title, String stepContent, TestReportJiraEntity testReportJiraEntity, TestReportJiraDTO testReportJiraDTO, ProjectProductionConfig projectProductionConfig) {
        return new String[0];
    }

    @Override
    public Map<String, String> getProjectList(ProjectProductionConfig projectProductionConfig) {
        // TODO 接入tapd的bug系统
        return null;
    }

    @Override
    public Map<String, String> getProductionList(ProjectProductionConfig projectProductionConfig) {
        // TODO 接入tapd的bug系统
        return null;
    }

    @Override
    public Map<String, String> getPriorityList(ProjectProductionConfig projectProductionConfig) {
        // TODO 接入tapd的bug系统
        return null;
    }

}
